<?php
function gourl_control() {
	global $wpdb;
	$gourl = home_url('/') . 'go/';
?>
<div id="gourl" class="mywrap">
<style type="text/css">
.mywrap {
	margin: 20px 20px 0 0;
}
.mywrap .notice {
	margin: 20px 0 10px 0;
	padding: .8em;
}
.mywrap .entry {
	background-color: #fff;
	padding: 20px;
	border: 1px solid #e5e5e5;
	border-radius: 4px;
}
.mywrap .tablenav {
	height: auto;
}
.mywrap .tablenav form {
	display: inline-block;
}
.mywrap .tablenav .button-primary {
	vertical-align: top;
}
.mywrap table {
	border-radius: 4px;
}
@media screen and (max-width: 782px) {
	.mywrap .button-primary {
		min-height: 40px;
	}
}
</style>
<?php
if(isset($_GET['action']) && 'edit' == $_GET['action']) :
	$urlid = isset($_GET['id']) ? (int) $_GET['id'] : 0;
	$urlrow = '';
	$url = '';
	$code = '';
	$note = '';
	if($urlid) {
		$urlrow = $wpdb->get_row($wpdb->prepare("SELECT * FROM gourls WHERE ID=%d LIMIT 1", $urlid));
	}
	if(is_object($urlrow)) {
		$edittitle = '编辑URL';
		$url = esc_url($urlrow->url);
		$code = $urlrow->code;
		$note = $urlrow->note;
		$urlparam = '&amp;id=' . $urlid;
	} else {
		$edittitle = '添加URL';
		$urlparam = '';
	}

	if(isset($_POST['action']) && 'save' == $_POST['action']) {
		$data['url'] = wp_specialchars_decode(rtrim($_POST['url'], '/'));
		$data['url'] = esc_url_raw($data['url']);
		$data['code'] = sanitize_text_field($_POST['code']);
		if($data['code'] == $code) {
			unset($data['code']);
		} else {
			$data['code'] = gourl_generate_code($data['code']);
			$code = $data['code'];
		}

		$data['note'] = sanitize_text_field($_POST['note']);
		
		if(is_object($urlrow)) {
			$wpdb->update('gourls', $data, [
				'ID' => $urlid
			]);

			if(!empty($data['code'])) {
				wp_cache_delete(md5($data['url']), 'gourl');
			}
		} else {
			$wpdb->insert('gourls', $data);
		}

		$url = esc_url($data['url']);
		$note = $data['note'];

		echo '<div class="notice is-dismissible updated">保存URL成功：' . $gourl . $code . '</div>';
	}
?>

<form class="entry" method="post" action="<?php echo admin_url('options-general.php?page=gourl&amp;action=edit' . $urlparam); ?>">
	<h2><?php echo $edittitle; ?></h2>
	<p class="description">文章内容中的站外链接将自动转换为短链接，无需手动添加。</p>
	<table class="form-table">
		<tbody>
			<tr>
				<th scope="row"><label for="url">URL地址</label></th>
				<td>
					<input id="url" name="url" class="regular-text code" type="url" value="<?php echo $url; ?>" required>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="code">短代码</label></th>
				<td>
					<input id="code" name="code" class="regular-text code" type="text" maxlength="16" value="<?php echo $code; ?>">
					<p class="description">字母或数字，不超过16个字符，不填写则随机生成。</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="note">备注</label></th>
				<td>
					<input id="note" name="note" class="regular-text code" type="text" value="<?php echo $note; ?>">
				</td>
			</tr>
		</tbody>
	</table>
	<p class="submit">
		<input type="submit" class="button-primary" name="action" value="保存URL">
		<input type="hidden" class="button" name="action" value="save">
		<a class="button" href="<?php echo admin_url('options-general.php?page=gourl'); ?>">&laquo; 返回</a>
	</p>
</form>

<?php else : 

$paged = isset($_GET['pn']) ? (int) $_GET['pn'] : 1;
$limit = 20;
$start = ($paged-1)*$limit;
$list = array();

//删除网址
if(isset($_GET['action']) && 'delete' == $_GET['action']) {
	$delid = isset($_GET['id']) ? (int) $_GET['id'] : 0;
	$delurl = $wpdb->get_var($wpdb->prepare("SELECT url FROM gourls WHERE ID=%d LIMIT 1", $delid));
	if($delurl) {
		$wpdb->delete('gourls', array(
			'ID' => $delid,
		));
		wp_cache_delete(md5($delurl), 'gourl');
		echo '<div class="notice is-dismissible updated">删除URL成功！</div>';
	} else {
		echo '<div class="notice is-dismissible error">删除URL失败！</div>';
	}
}

$search = '';
$urlparam = '';
$where = '';
if(!empty($_GET['q'])) {
	$search = trim($_GET['q']);
	$where = $wpdb->prepare(" WHERE url LIKE %s OR note LIKE %s", "%$search%", "%$search%");
	$urlparam = '&amp;q=' . $search;
}
$count = $wpdb->get_var("SELECT COUNT(ID) FROM gourls {$where}");
if($count) {
	$list = $wpdb->get_results("SELECT * FROM gourls {$where} ORDER BY ID DESC LIMIT $start, $limit");
}

?>
	<div class="tablenav top">
		<form action="<?php echo admin_url('options-general.php'); ?>" method="get">
			<input type="hidden" name="page" value="gourl">
			<input type="search" name="q" placeholder="输入URL或备注搜索..." value="<?php echo $search; ?>">
			<input type="submit" class="button-primary" value="搜索">
		</form>
		<a class="button" href="<?php echo admin_url('options-general.php?page=gourl&amp;action=edit'); ?>">添加</a>
	</div>
	<table class="wp-list-table widefat fixed striped">
		<thead>
			<th scope="col" class="manage-column column-primary" width="50%">URL</th>
			<th scope="col" class="manage-column" width="10%">短代码</th>
			<th scope="col" class="manage-column" width="30%">备注</th>
			<th scope="col" class="manage-column" width="10%">操作</th>
		</thead>
		<tbody>
			<?php
			if($list) {
				foreach($list as $val) {
					echo '<tr>
							<td class="column-primary" data-colname="URL">
								<a href="' . esc_url($val->url) . '" target="_blank" rel="noopener nofollow">' . esc_url($val->url) . '</a>
								<button type="button" class="toggle-row"></button>
							</td>
							<td data-colname="短代码">
								<a href="' . $gourl . $val->code . '" target="_blank" rel="noopener nofollow">' . $val->code . '</a>
							</td>
							<td data-colname="备注">' . $val->note . '</td>
							<td data-colname="操作">
								<a class="button" href="' . admin_url('options-general.php?page=gourl&amp;action=edit&amp;id=' . $val->ID) . '">编辑</a>
								<a class="button" href="' . admin_url('options-general.php?page=gourl&amp;action=delete&amp;id=' . $val->ID) . '">删除</a>';
					echo '</td></tr>';
				}
			}
			?>
		</tbody>
	</table>
	
<?php
	if($count && $pagenav = gourl_pagenav($count, $limit, $paged, "options-general.php?page=gourl{$urlparam}")) {
		echo '<div class="tablenav bottom">' . $pagenav . '</div>';
	}
?>

<?php
	endif;
	echo '</div>';
}
?>
